package com.miku.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miku.blog.domain.WebsitePage;
import com.miku.blog.domain.pojo.ResponseResult;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.eume.HttpStatusEnum;
import com.miku.blog.service.WebsitePageService;
import com.miku.blog.mapper.WebsitePageMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
* @author Miku
* @description 针对表【tb_website_page】的数据库操作Service实现
* @createDate 2022-06-08 08:47:10
*/
@Service
public class WebsitePageServiceImpl extends ServiceImpl<WebsitePageMapper, WebsitePage>
    implements WebsitePageService{


    @Override
    public List<WebsitePage> getWebSitePageList(Integer webSiteId){
        LambdaQueryWrapper<WebsitePage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WebsitePage::getWebsiteId, webSiteId);
        List<WebsitePage> websitePageList = list(queryWrapper);
        return websitePageList;
    }


    @Override
    public ResponseResult getPages() {
        return ResponseResult.ok().setData(list());
    }


    @Override
    public ResponseResult updatePage(WebsitePage websitePage) {
        String pageCover = websitePage.getPageCover();
        String pageLabel = websitePage.getPageLabel();
        String pageName = websitePage.getPageName();
        if (!StringUtils.hasText(pageCover) || !StringUtils.hasText(pageLabel) || !StringUtils.hasText(pageName)){
            return new ResponseResult(HttpStatusEnum.PARAM_ERROR);
        }
        Integer id = websitePage.getId();
        if (Objects.isNull(id)){
            websitePage.setWebsiteId(SystemConstance.WEBSITE_ID);
            if (!save(websitePage)){
                return new ResponseResult(HttpStatusEnum.INSERT_DATA_ERROR);
            }
        }else {
            if (!updateById(websitePage)){
                return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
            }
        }
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult delPage(Integer pageId) {
        if (!removeById(pageId)) {
            return new ResponseResult(HttpStatusEnum.REMOVE_DATA_ERROR);
        }
        return ResponseResult.ok();
    }
}




